import Project from '../model/project.js';
import Page from '../model/page.js';

const attributes = {
    exclude: ['deletedAt', 'updatedAt']
}

export default class ProjectController {

    projects() {
        return Project.findAll({ attributes });
    }

    project(id) {
        return Project.findByPk(
            id,
            {
                attributes,
                include: [{
                    model: Page
                }]
            });
    }

    async createProject(payload) {
        const project = await Project.create({
            name: payload.name,
            desc: payload.desc,
            coverUrl: payload.coverUrl
        });
        return project.id;
    }

    async updateProject(id, payload) {
        await Project.update(
            {
                name: payload.name,
                desc: payload.desc,
                coverUrl: payload.coverUrl
            },
            {
                where: {
                    id
                }
            }
        );
    }

    async deleteProject(id) {
        await Project.destroy({
            where: {
                id
            }
        });
    }

}
